﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;


namespace Raw1
{
    public class MonitorManager //: IMonitorManager
    {
        #region Private members
        private List<IMonitor> _monitorList;
        private bool _isRunning = false;
        #endregion

        #region Public Properties
        public bool IsRunning
        {
            get { return _isRunning; }
            set { _isRunning = value; }
        }

        public List<IMonitor> Monitors 
        {
            get
            { return _monitorList; }
        }

        #endregion

        #region Events

        public delegate void MonitorEventHandler(object sender, MonitorEventArgs e);

        public event MonitorEventHandler MonitorAddedEvent;
        public event MonitorEventHandler MonitorRemovedEvent;
        public event EventHandler MonitoringStaredEvent;
        public event EventHandler MonitoringStoppedEvent;
        public event EventHandler MonitorChangedEvent;


        public void OnMonitorAdded(IMonitor monitor)
        {

            MonitorEventHandler handler = MonitorAddedEvent;
            if (handler != null)
            {
                MonitorEventArgs e = new MonitorEventArgs(monitor);
                handler(this, e);
            }
        }

        public void OnMonitorRemoved(IMonitor monitor)
        {
            MonitorEventHandler handler = MonitorRemovedEvent;
            if (handler != null)
            {
                MonitorEventArgs e = new MonitorEventArgs(monitor);
                handler(this, e);
            }
        }

        public void OnMonitorStarted()
        {
            EventHandler handler = MonitoringStaredEvent; ;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }

        public void OnMonitorStopped()
        {
            EventHandler handler = MonitoringStoppedEvent; ;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }



        #endregion

        #region Contructor
        public MonitorManager()
        {
            _monitorList = new List<IMonitor>();
        }
        #endregion

        #region Public Methods
        public void Subscribe(IMonitor monitor)
        {
            if (_monitorList.Any(m => m.MonitorType == monitor.MonitorType))
            {
                //Already there
            }
            else
            {
                monitor.MonitorChangedEvent += monitor_MonitorChangedEvent;
                _monitorList.Add(monitor);


                OnMonitorAdded(monitor);
            }
        }

        void monitor_MonitorChangedEvent(object sender, EventArgs e)
        {
            EventHandler handler = MonitorChangedEvent; ;
            if (handler != null)
            {
                handler(sender, e);
            }
        }

        public void Unsubscribe(IMonitor monitor)
        {
            if (_monitorList.Any(m => m.MonitorType == monitor.MonitorType))
            {
                //not in list
            }
            else
            {
                _monitorList.Remove(monitor);
                OnMonitorRemoved(monitor);
            }
        }

        public void Start()
        {
            foreach(var _monitor in _monitorList)
            {
                _monitor.RunWatcher();
            }

            _isRunning = true;
            OnMonitorStarted();
        }

        public void Stop()
        {
            _isRunning = false;
            OnMonitorStopped();
        }

        public void Notify() { }

        #endregion

    }

}
